import React, { Component } from 'react'
import {connect} from 'react-redux'
class Login extends Component {
    state={
        username:''
    }
    changename=(e)=>{
        this.setState({
            username:e.target.value
        })
    }
    login=()=>{
        this.props.setuser(this.state.username)
        //把用户名存储到本地
        localStorage.setItem('yijuuser',this.state.username)
        this.props.history.goBack();
    }
    render() {
        return (
            <div>
                用户名：<input type="text" value={this.state.username} onChange={this.changename}/>
                <button onClick={this.login}>登陆</button>
            </div>
        )
    }
}
function mapStateToProps(state){
    return{
        user:state.user
    }
} 
function mapDispatchToProps(dispatch){
    return{
       setuser:(data)=>dispatch({type:'SETUSER',playload:data})
    }
}
export default connect(mapStateToProps,mapDispatchToProps)(Login)
